<template>
<el-dialog title="生成入库单" :close-on-click-modal="false" :visible.sync="visible" width="90%">
    <el-form :model="dataForm" label-width="100px" inline>
        <el-form-item label="供应商">
            <el-input v-model="dataForm.supplierName" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="订货时间">
            <el-input v-model="dataForm.orderDateShow"></el-input>
        </el-form-item>
        <el-form-item label="发货时间">
            <el-input v-model="dataForm.sendDateShow"></el-input>
        </el-form-item>
        <el-form-item label="发货信息">
            <el-input v-model="dataForm.sendInfo"></el-input>
        </el-form-item>
    </el-form>
    <el-table :data="dataForm.bookList" border style="width: 100%;">
        <el-table-column prop="purchaseId" header-align="center" label="采购申请编号" align="center" width="110"></el-table-column>
        <el-table-column header-align="center" align="center" label="封面" width="120">
            <template slot-scope="scope">
                <img v-if="scope.row.book.imgUrl" style="width: 100px; height: 100px" :src="scope.row.book.imgUrl" />
                <span v-else></span>
            </template>
        </el-table-column>
        <el-table-column prop="book.name" header-align="center" align="center" label="图书名称"></el-table-column>
        <el-table-column prop="book.type" header-align="center" align="center" label="分类" width="50"></el-table-column>
        <el-table-column prop="book.isbn" header-align="center" align="center" label="ISBN" width="125"></el-table-column>
        <el-table-column prop="book.author" header-align="center" align="center" label="作者"></el-table-column>
        <el-table-column prop="num" header-align="center" label="入库数量" width="80"></el-table-column>
        <el-table-column header-align="center" align="center" label="入库仓库">
            <template slot-scope="scope">
                <el-select v-model="scope.row.storeId" placeholder="请选择仓库">
                    <el-option v-for="item in storeList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
                </el-select>
            </template>
        </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
</el-dialog>
</template>

<script>
import DateUtil from "@/utils/date";

export default {
    data() {
        return {
            visible: false,
            storeList: [{
                id: 1,
                desc: "仓1",
            }, ],
            putin: {},
            dataForm: {},
            dataListSelections: [],
        };
    },
    methods: {
        init(id) {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() => {
                if (this.dataForm.id) {
                    this.$http({
                        url: this.$http.adornUrl(
                            `/generator/torder/info/${this.dataForm.id}`
                        ),
                        method: "get",
                        params: this.$http.adornParams(),
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            this.dataForm = data.tOrder;
                            this.dataForm.sendDateShow = DateUtil.formatDate(
                                this.dataForm.sendDate
                            );
                            this.dataForm.orderDateShow = DateUtil.formatDate(
                                this.dataForm.orderDate
                            );
                            this.dataForm.bookList.forEach(function (item) {
                                if (item.book.img) {
                                    item.book.imgUrl =
                                        "http://localhost:8080/wenyuan/generator/tbook/img/" + item.book.id;
                                }
                            });
                        }
                    });
                }
            });
        },

        // 表单提交
        dataFormSubmit() {
            console.log(this.dataForm.bookList)
            let bookList = [];
            this.dataForm.bookList.forEach(function (item) {
                let book = {
                    bookId: item.book.id,
                    storeId: item.storeId,
                    num: item.num
                }
                bookList.push(book);
            });
            this.$http({
                url: this.$http.adornUrl(`/generator/tputin/save`),
                method: "post",
                data: this.$http.adornData({
                    orderId: this.dataForm.id,
                    bookList: bookList
                }),
            }).then(({
                data
            }) => {
                if (data && data.code === 0) {
                    this.$message({
                        message: "操作成功",
                        type: "success",
                        duration: 1500,
                        onClose: () => {
                            this.visible = false;
                            this.$emit("refreshDataList");
                        },
                    });
                } else {
                    this.$message.error(data.msg);
                }
            });
        },
    },
};
</script>
